package controller

import (
	json2 "encoding/json"
	"github.com/google/uuid"
	"log"
	"passwordBook/entity"
	"passwordBook/sql"
	"passwordBook/util"
	"time"
)

type PwController struct {
	BasicController
}

func doEncrypt(method string, pw *entity.PwItem, p *PwController) {
	if method == "AES" {
		sec, err := util.AESEncrypt(pw.Password, pw.Book.Config.EncryptKey)
		if err != nil {
			log.Println(err)
			p.Message = Message{Code: 500, Msg: "更新失败：" + err.Error()}
			p.JSONResp(p.Message)
		}
		pw.Password = sec
	} else if method == "RSA" {
		sec, err := util.RSAEncrypt(pw.Password, pw.Book.Config.PublicKeyFilePath)
		if err != nil {
			log.Println(err)
			p.Message = Message{Code: 500, Msg: "更新失败：" + err.Error()}
			p.JSONResp(p.Message)
		}
		pw.Password = sec
	}
}
func (p *PwController) List() {
	var json []byte
	json = p.Ctx.Input.RequestBody
	var mapper map[string]string
	json2.Unmarshal(json, &mapper)
	pws := []entity.PwItem{}
	sql.GLOBAL_DB.Db.Preload("Book").Where("book_id=?", mapper["id"]).Find(&pws, &entity.PwItem{})
	p.Message = Message{Code: 200, Data: pws, Msg: "获取成功"}
	p.JSONResp(p.Message)

}
func (p *PwController) Create() {
	data := entity.PwItem{}
	p.BindJSON(&data)
	book := entity.PwBook{Id: data.BookId}
	data.Id = uuid.New().String()
	sql.GLOBAL_DB.Db.Find(&book)
	data.Book = book
	method := data.Book.Config.EncryptMethod
	switch method {
	case "AES":
		{
			sec, err := util.AESEncrypt(data.Password, book.Config.EncryptKey)
			if err != nil {
				log.Println(err)
			}
			data.Password = sec
		}
	case "RSA":
		{
			sec, err := util.RSAEncrypt(data.Password, book.Config.PublicKeyFilePath)
			if err != nil {
				log.Println(err)
			}
			data.Password = sec
		}
	}
	book.UpdateTime = time.Now()
	sql.GLOBAL_DB.Db.Updates(&book)
	log.Printf("向密码本%s中添加了一条密码：欸嘿，不告诉你！", book.Id)
	sql.GLOBAL_DB.Db.Create(&data)
	p.Message = Message{Code: 200, Data: data, Msg: "创建成功"}
	p.JSONResp(p.Message)

}
func (p *PwController) ShowPwd() {
	body := p.Ctx.Input.RequestBody
	var reqbody map[string]string
	json2.Unmarshal(body, &reqbody)
	id := reqbody["id"]
	method := reqbody["method"]
	data := entity.PwItem{Id: id}
	sql.GLOBAL_DB.Db.Find(&data)
	book := entity.PwBook{Id: data.BookId}
	sql.GLOBAL_DB.Db.Find(&book)
	result := ""
	switch method {
	case "AES":
		{
			sec, err := util.AESDecrypt(data.Password, book.Config.EncryptKey)
			if err != nil {
				log.Println(err)
			}
			result = sec
		}
	case "RSA":
		{
			sec, err := util.RSADecrypt(data.Password, book.Config.PrivateKeyFilePath)
			if err != nil {
				log.Println(err)
			}
			result = sec
		}
	}

	log.Println("获取了明文")
	p.Message = Message{Code: 200, Data: result, Msg: "获取成功"}
	p.JSONResp(p.Message)

}
func (p *PwController) Update() {
	var pw entity.PwItem
	p.BindJSON(&pw)
	var newPw = pw.Password
	sql.GLOBAL_DB.Db.Preload("Book").Find(&pw)
	pw.Password = newPw
	method := pw.Book.Config.EncryptMethod
	doEncrypt(method, &pw, p)
	sql.GLOBAL_DB.Db.Updates(&pw)
	pw.Book.UpdateTime = time.Now()
	sql.GLOBAL_DB.Db.Updates(&pw.Book)
	p.Message = Message{Code: 200, Msg: "更新成功", Data: pw}
	p.JSONResp(p.Message)
}
func (p *PwController) Delete() {
	id := p.Ctx.Input.Param(":id")
	sql.GLOBAL_DB.Db.Delete(&entity.PwItem{Id: id})
	p.Message = Message{Code: 200, Msg: "删除成功"}
	p.JSONResp(p.Message)
}
